/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import POJO.TiecCuoi;
import UTIL.HibernateUtil;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author ChanhTin
 */
public class TiecCuoiDAO {
     
    /**
     * @desc lay danh sach tiec cuoi
     * @return 
     */
    public static List<TiecCuoi> layDanhSach() {
        List<TiecCuoi> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select tc from TiecCuoi tc";
            org.hibernate.Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;  
    }
    
    /**
     * @desc tim tiec cuoi theo ma tiec cuoi
     * @param maTiecCuoi
     * @return 
     */
    public static TiecCuoi layThongTinTiecCuoi(int maTiecCuoi) {
        TiecCuoi tc = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            tc = (TiecCuoi) session.get(TiecCuoi.class, maTiecCuoi);
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
            session.close();
        }
        return tc;
    }
    
    /**
     * @desc them tiec cuoi
     * @param tc
     * @return 
     */
    public static boolean themTiecCuoi(TiecCuoi tc) throws HibernateException{
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (TiecCuoiDAO.layThongTinTiecCuoi(tc.getMaTiecCuoi())!=null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.save(tc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag;
    }
    
    /**
     * @desc cap nhat thong tin tiec cuoi
     * @param tc
     * @return 
     */
    public static boolean capNhatThongTinTiecCuoi(TiecCuoi tc) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        if (TiecCuoiDAO.layThongTinTiecCuoi(tc.getMaTiecCuoi()) == null) { 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.update(tc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
           //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    } 
    
    /**
     * @desc xoa tiec cuoi
     * @param maTiecCuoi
     * @return 
     */
    public static boolean xoaTiecCuoi(int maTiecCuoi) throws HibernateException{ 
        Session session = HibernateUtil.getSessionFactory().openSession(); 
        TiecCuoi tc = TiecCuoiDAO.layThongTinTiecCuoi(maTiecCuoi); 
        if(tc==null){ 
            return false; 
        }
        boolean flag = true;
        Transaction transaction = null; 
        try { 
            transaction = session.beginTransaction(); 
            session.delete(tc); 
            transaction.commit(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            transaction.rollback(); 
            //System.err.println(ex);
            flag = false;
        } finally { 
            session.close(); 
        } 
        return flag; 
    }
}
